﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace Defect_Tracking_System.Models
{
    public class EmailSender
    {
        private static String Gmail = "defecttrackingsystem2011@gmail.com";
        private static String Password = "robo8590";

        private static EmailSender instance;

        private Dictionary<String, MailMessage> Emails;

        private EmailSender()
        {
            Emails = new Dictionary<String, MailMessage>();
        }

        public static EmailSender GetInstance()
        {
            if (instance == null)
            {
                instance = new EmailSender();
            }
            return instance;
        }

        public void AddNewEmail(String From, String To, String Subject, String Message)
        {
            if (!Emails.Keys.Contains(To))
            {
                MailMessage message = new MailMessage();
                MailAddress fromAddress = new MailAddress(From, From, System.Text.Encoding.UTF8);

                message.To.Add(To);
                message.From = fromAddress;
                message.Subject = Subject;
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Body = "<h1>" + Subject + "</h1>" + Message;

                Emails.Add(To, message);
            }
            else
            {
                Emails[To].Body = Emails[To].Body + "<h1>" + Subject + "</h1>" + Message;
            }
        }

        public void AddNewEmail(String To, String Subject, String Message)
        {
            AddNewEmail(Gmail, To, Subject, Message);
        }

        private void Send(String From, String To, String Subject, String Message)
        {
            SmtpClient smtpClient = new SmtpClient();
            MailMessage message = new MailMessage();

            try
            {
                MailAddress fromAddress = new MailAddress(From, From);

                // You can specify the host name or ipaddress of your server

                // Default in IIS will be localhost 

                smtpClient.Host = "localhost";

                //Default port will be 25

                smtpClient.Port = 25;

                //From address will be given as a MailAddress Object

                message.From = fromAddress;

                // To address collection of MailAddress

                message.To.Add(To);
                message.Subject = Subject;

                //Body can be Html or text format

                //Specify true if it  is html message

                message.IsBodyHtml = true;

                // Message body content

                message.Body = Message;

                // Send SMTP mail

                smtpClient.Send(message);

            }
            catch (Exception ex)
            {
            }
        }

        private void SendWithGmail(String Gmail, String Password, String To, String Subject, String Message)
        {
            SmtpClient client = new SmtpClient();
            //client.Port = 465; // Gmail works on this port
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            client.Credentials = new System.Net.NetworkCredential(Gmail, Password);

            MailMessage message = new MailMessage();
            MailAddress fromAddress = new MailAddress(Gmail, Gmail, System.Text.Encoding.UTF8);

            message.To.Add(To);
            message.From = fromAddress;
            message.Subject = Subject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = true;
            message.Body = Message;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Priority = MailPriority.High;                  

            try
            {
                client.Send(message);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }

                throw new Exception(errorMessage);
            }
        }

        private void Send(String To, String Subject, String Message)
        {
            SendWithGmail("defecttrackingsystem2011@gmail.com", "robo8590", To, Subject, Message);
        }

        private void Send(MailMessage Email)
        {
            SmtpClient client = new SmtpClient();
            //client.Port = 465; // Gmail works on this port
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Gmail works on Server Secured Layer
            client.Credentials = new System.Net.NetworkCredential(Gmail, Password);

            try
            {
                client.Send(Email);
            }
            catch (Exception ex)
            {
                Exception ex2 = ex;
                string errorMessage = string.Empty;
                while (ex2 != null)
                {
                    errorMessage += ex2.ToString();
                    ex2 = ex2.InnerException;
                }

                throw new Exception(errorMessage);
            }
        }

        public void Send()
        {
            foreach (MailMessage Email in Emails.Values)
            {
                //Send(Email);
            }
            Emails = new Dictionary<string, MailMessage>();
        }
    }
}